package com.android.clinic.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.clinic.app.R;
import com.android.clinic.app.entity.DoctorInfo;
import com.android.clinic.app.glide.ImageLoaderUtils;
import com.android.clinic.app.utils.Utils;

/**
 * Created by cheng on 2018/2/9.
 */
public class DoctorAdapter extends BaseListAdapter<DoctorInfo> {

    private Activity activity;

    public DoctorAdapter(Context context, int... resource) {
        super(context, R.layout.item_list_famous_doctor);
        activity = (Activity) context;
    }


    @Override
    protected Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex) {
        return new ViewHolder(convertView);
    }

    @Override
    protected void setupBindViewHolder(final DoctorInfo object, Object viewHolder, int position, int typeIndex) {
        ViewHolder holder = (ViewHolder) viewHolder;
        holder.tvName.setText(object.getName());
        holder.tvHospital.setText(object.getHospital());
        holder.tvPosition.setText(object.getPosition());
        holder.tvDeptName.setText(object.getDeptName());
        holder.tvReservation.setText(object.getReservation());
        holder.tvEvaluation.setText(object.getEvaluation());
        holder.tvInquirePrice.setText(activity.getString(R.string.doctor_inquire_price_text, object.getInquirePrice()));

        if (object.getImgUrl().contains(".jpg") && !Utils.isEmpty(object.getImgUrl())) {
            holder.ivHead.setVisibility(View.VISIBLE);
            holder.layDefault.setVisibility(View.GONE);
            ImageLoaderUtils.getInstance().loadHeadImage(object.getImgUrl(), holder.ivHead);
        } else {
            holder.ivHead.setVisibility(View.GONE);
            holder.layDefault.setVisibility(View.VISIBLE);
            holder.tvDefaultName.setText(object.getName());
        }

        if (!Utils.isEmpty(object.getLabel())) {
            holder.layLabel.setVisibility(View.VISIBLE);
            String[] labelArray = object.getLabel().split(",");
            int labelSize;
            if (labelArray.length > 2) {
                labelSize = 2;
            } else {
                labelSize = labelArray.length;
            }
            holder.layLabel.removeAllViews();
            for (int i = 0; i < labelSize; i++) {
                View labelView = LinearLayout.inflate(activity,
                        R.layout.item_list_famous_doctor_label, null);

                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.setMargins(0, 0, 10, 0);
                labelView.setLayoutParams(lp);

                TextView tvLabel = (TextView) labelView.findViewById(R.id.tv_label);
                tvLabel.setText(labelArray[i]);
                holder.layLabel.addView(labelView);
            }
        } else {
            holder.layLabel.setVisibility(View.GONE);
        }

    }

    class ViewHolder extends BaseViewHolder {

        ImageView ivHead;
        TextView tvName, tvPosition, tvHospital, tvDefaultName, tvDeptName, tvReservation, tvEvaluation,
            tvInquirePrice;
        LinearLayout layLabel;
        FrameLayout layDefault;

        public ViewHolder(View itemView) {
            super(itemView);
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvPosition = (TextView) itemView.findViewById(R.id.tv_position);
            tvHospital = (TextView) itemView.findViewById(R.id.tv_hospital);
            layLabel = (LinearLayout) itemView.findViewById(R.id.layout_label);
            tvDefaultName = (TextView) itemView.findViewById(R.id.tv_default_name);
            layDefault = (FrameLayout) itemView.findViewById(R.id.layout_default);
            tvDeptName = (TextView) itemView.findViewById(R.id.tv_dept_name);
            tvReservation = (TextView) itemView.findViewById(R.id.tv_reservation);
            tvEvaluation = (TextView) itemView.findViewById(R.id.tv_evaluation);
            tvInquirePrice = (TextView) itemView.findViewById(R.id.tv_inquire_price);
        }
    }

}
